// CVisNiftiImageIO.h
// Developed by Yang Yu (yuy@janelia.hhmi.org), Dec 20, 2011
//

#ifndef __CVISNIFTIIMAGEIO_H__
#define __CVISNIFTIIMAGEIO_H__

#include "CVisUtility.h"

extern "C" {
#include "nifti1_io.h"
};

// This class is modified from the itk itkNiftiImageIO class

//
#define MIN_HEADER_SIZE 348
#define NII_HEADER_SIZE 352

#define FILE_TYPE_NIFTI 100
#define FILE_TYPE_NIFTI_GZ 101

//
class NiftiImageIO
{

public:
    NiftiImageIO();
    ~NiftiImageIO();

public:

    // reading
    /** Determine if the file can be read with this ImageIO implementation. */
    bool canReadFile(char *FileNameToRead);

    /** loading image to memory */
    bool read(void *&buffer);

    // writing
    /** Determine if the file can be written with this ImageIO implementation. */
    bool canWriteFile(char *FileNameToWrite);

    /** Writes the data to disk from the memory buffer provided. */
    void write(const void *buffer, LONG64 sx, LONG64 sy, LONG64 sz, LONG64 sc, LONG64 st, int datatype);

    //
    char* getFileName();
    void setFileName(char* FileName);
    
    // compressed ?
    int isCompressed(const char * filename);
    
    // 
    LONG64 getDimx();
    LONG64 getDimy();
    LONG64 getDimz();
    LONG64 getDimc();
    LONG64 getDimt();

    void setDimx(LONG64 x);
    void setDimy(LONG64 y);
    void setDimz(LONG64 z);
    void setDimc(LONG64 c);
    void setDimt(LONG64 t);

    int getDataType();
    void setDataType(int dt);

private:
    bool mustRescale();
    void setPixelType(IOPixelType pt);

private:
    znzFile m_GZFile;
    nifti_image *m_NiftiImage;

    double m_RescaleSlope;
    double m_RescaleIntercept;

    bool m_LegacyAnalyze75Mode;

    char* m_FileName;

    IOPixelType m_PixelType;
    IOComponentType m_ComponentType;

    LONG64 dimx, dimy, dimz, dimc, dimt; // dimension
    double dx, dy, dz, dc, dt; // spacing
};

#endif // __CVISNIFTIIMAGEIO_H__
